﻿using System.Diagnostics.Contracts;

namespace System.Reactive.Linq
{
	public static partial class Observable2
	{
		/// <summary>
		/// Invokes the actions for their side-effects on each value in the observable sequence and blocks till the sequence is terminated.
		/// </summary>
		/// <typeparam name="TLeft">Type of the left notification channel.</typeparam>
		/// <typeparam name="TRight">Type of the right notification channel.</typeparam>
		/// <param name="source">The observable for which a subscription is created.</param>
		/// <param name="onNextLeft">The handler of notifications in the left channel.</param>
		/// <param name="onNextRight">The handler of notifications in the right channel.</param>
		public static void ForEach<TLeft, TRight>(
			this IObservable<Either<TLeft, TRight>> source,
			Action<TLeft> onNextLeft,
			Action<TRight> onNextRight)
		{
			Contract.Requires(source != null);
			Contract.Requires(onNextLeft != null);
			Contract.Requires(onNextRight != null);

			source.ForEach(value => value.Switch(onNextLeft, onNextRight));
		}
	}
}